package com.qq.googleplay.ui.widget;

import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.ListView;

/**
 * ============================================================
 * 版 权 ： Google互联网有限公司版权所有 (c) 2016
 * 作 者 : 陈冠杰
 * 版 本 ： 1.0
 * 创建日期 ：2016/7/1 10:05
 * 描 述 ：
 * 修订历史 ：
 * ============================================================
 **/
public class CustomListView extends ListView {

    private int mTouchSlop;
    private float mPrevX;
    private float mPrevY;

    public CustomListView(Context context) {
        super(context);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop() + 50;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        switch (MotionEventCompat.getActionMasked(ev)) {

            case MotionEvent.ACTION_DOWN:
                mPrevX = MotionEvent.obtain(ev).getX();
                mPrevY = MotionEvent.obtain(ev).getY();
                break;

            case MotionEvent.ACTION_MOVE:
                final float eventX = ev.getX();
                final float eventY = ev.getY();
                float xDiff = Math.abs(eventX - mPrevX);
                float yDiff = Math.abs(eventY - mPrevY);

                if (xDiff > mTouchSlop && Math.abs(xDiff) > Math.abs(yDiff)) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
        }
        return super.dispatchTouchEvent(ev);
    }

}
